<?php
	class domini extends ini{
		var $unset_keys = array('active', 'dbObj');
		var $critical_keys = array('DOM_ID', 'SMERNEL', 'NAME');
		var $write_if_no_read = true;
	}

	class Domain{
		
		protected $DOM_ID;
		protected $SMERNEL;
		
		protected $NAME;
		protected $DESKTOP;
		
		protected $DOCROOT;
		protected $DESKDIR;
		protected $TEMPDIR;
		protected $LOG_DIR;
		
		protected $ALIASES;
		
		protected $active;
		
		function __construct($DOM_ID){
			$this->DOM_ID = $DOM_ID;
			// Are we the running domain?
			$this->active = (!sn()->dom_up());
			// Load the settings
			$this->loadconfig($this->active);
			// TEMPDIR is constant
			if(!is_dir(SHR_ROOT.'/domtemp')) mkdir(SHR_ROOT.'/domtemp', 0755);
			$this->TEMPDIR = SHR_ROOT.'/domtemp/'.$this->DOM_ID;
			// If we're not active then we're done constructing.
			if(!$this->active) return;
			// We're the active domain. Set the constants
			$this->set_constants();
			// Go to desktop diretory
			chdir(DOM_DESKDIR);
			// Let Smernel know we're done constructing, our members can be called
			sn()->dom_up(true);
		}
		
		function __get($k){
			if(isset($this->$k)) return $this->$k;	
		}
		
		protected function loadconfig($from_global=false){
			$a = ($from_global) ? array_merge($this->defaults(), $GLOBALS['DOM_INI']) : $this->get_config();
			foreach($a as $k=>$v) $this->$k = $this->parse_conf_constants($v);
		}
		
		function get_config(){
			return new domini($this->config_file(), $this->defaults());
		}
		
		function config_file(){
			return ETC_DIR.'/conf/'.$this->DOM_ID;
		}
				
		function defaults(){
			return array(
				'DOM_ID'=>$this->DOM_ID,
				'SMERNEL'=>SMERNEL,
				'NAME'=>$this->DOM_ID,
				'DESKTOP'=>'smrde.'.$this->DOM_ID,
				'DOCROOT'=>'%{DOC_DIR}%/'.$this->DOM_ID,
				'DESKDIR'=>'%{DSK_DIR}%/'.$this->DOM_ID,
				'LOG_DIR'=>'%{DSK_DIR}%/'.$this->DOM_ID.'/logs',
				'ALIASES'=>array('ALIAS'=>array())
			);
		}
		
		// parse_constants is run having loaded the ini and interprets those variables that apply (first char is %)
		// It can also be called statically
		function parse_conf_constants($v){
			if(!is_string($v) || (substr($v, 0, 1) != '%')) return $v;
			$a = smuoi_orientation_constants();
			return str_replace(array_keys($a), $a, $v);	
		}
		
		// set_constants is called if this is the active domain
		// Other than it deals with PHP gobal constants, it is not at all related to parse_constants
		protected function set_constants(){
			foreach(array('NAME', 'DOCROOT', 'DESKDIR', 'TEMPDIR', 'DESKTOP', 'LOG_DIR') as $V) define("DOM_$V", $this->$V);
			ini_set('error_log', DOM_LOG_DIR.'/'.date('Y-m-d@H').'h.log');
		}
				
		// Names and aliases
		function names($co=false){
			$o = $co ? $co : $this;
			return array_merge(array($o->NAME, $o->DESKTOP), $this->aliases($co));	
		}
		
		function aliases($co=false){
			$o = $co ? $co : $this;
			return (is_array($a = $o->ALIASES) && isset($a['ALIAS'])) ? $a['ALIAS'] : array();
		}
		
		// run() is very simple indeed
		function run(){
			if(!$this->active) return;
			if(smuoi_xtn_is_run()){
				// Hand control to Documents class
				Documents()->run();
				exit;
			}
			// If we introduced a buffer, delete it
			$ol = ini_get('output_buffering') ? 1 : 0;
			while(ob_get_level() > $ol) ob_end_clean();
			if(smuoi_xtn_permit_handover()) return;
			trigger_error('Handover forbidden to "'.XTN.'". Check your SMUOI directives.', E_USER_ERROR);
			exit;
		}
		
		function getDir($k='docs'){
			switch(strtolower($k)){
				case 'docs':
					return $this->DOCROOT;
				case 'desk':
					return $this->DESKDIR;
				case 'temp':
					return $this->TEMPDIR;
			}
		}
		
		function uri($k=false){
			switch($k){
				case 'desk':
					return 'http://'.$this->DESKTOP;
				case 'temp':
					return 'http://'.$this->DESKTOP.'/@public/domtemp/'.$this->DOM_ID;
				default:
					return 'http://'.$this->NAME;
			}
		}
		
		function Database(){
			return sn()->proxy($this->DOM_ID);
		}
		
		function Fmgr(){
			return ($db = $this->Database()) ? $db->package('Fmgr') : false;
		}
	
	}


